<?php
/**
 * User: 魔十七
 * Date: 2020/11/6
 * Time: 8:42
 */

namespace Samecity\Controller;

use Think\Controller;
class AdsController extends Controller
{
    //获取首页轮播
    public function getCarouselPic(){
        $list=M('ads')->where('is_show=1')->limit('3')->order('id desc')->select();

        foreach ($list as $k=>$v){
            $v['pic']=getPic($v['pic']);
            $list[$k]=$v;
        }
        $arr = array(
            "code"=>0,
            "msg"=>'',
            "data"=>$list,
        );
        $this->ajaxReturn($arr);
    }
    
     //获取资料
    public function getMaterial(){
       
        $get = $_GET;
        $where['city']=$get['city'];
        //顺风车
        $freeride = M('freeride')->where($where)->order('add_time desc')->select();
        foreach ($freeride as $k=>$v){
            $v['images']=getPic($v['images']);
            $freeride[$k]=$v;
        }
        //招聘
        $recruit = M('recruit')->where($where)->order('add_time desc')->select();
        foreach ($recruit as $k=>$v){
            $u['id'] = $v['uid'];
            $user = M('user')->where($u)->find();
            $v['tx']=$user['avatar'];
            $v['images'] = explode(',',$v['images']);
            // var_dump($images);die;
            // var_dump($v['images']);die;
            // $v['images']=getPic($v['images']);
            $v['add_time']=date('Y-m-d',$v['add_time']);
            $recruit[$k]=$v;
        }
        //本地服务
        $local = M('local')->where($where)->order('add_time desc')->select();
        foreach ($local as $k=>$v){
            $v['images']=getPic($v['images']);
            $local[$k]=$v;
        }
        //房屋信息
        $house= M('house')->where($where)->order('add_time desc')->select();
        foreach ($house as $k=>$v){
            $v['images']=getPic($v['images']);
            $house[$k]=$v;
        }
        //生意转让
        $business_transfer = M('business_transfer')->where($where)->order('add_time desc')->select();
        foreach ($business_transfer as $k=>$v){
            $v['images']=getPic($v['images']);
            $business_transfer[$k]=$v;
        }
        //汽车交易 + 二手物品
        $car_trading = M('car_trading')->where($where)->order('add_time desc')->select();
        foreach ($car_trading as $k=>$v){
            $v['images']=getPic($v['images']);
            $car_trading[$k]=$v;
        }
        //发布摄影
        $photography = M('photography')->where($where)->order('add_time desc')->select();
        foreach ($photography as $k=>$v){
            $v['images']=getPic($v['images']);
            $photography[$k]=$v;
        }
        //装修定制
        $decoration_customization = M('decoration_customization')->where($where)->order('add_time desc')->select();
        foreach ($decoration_customization as $k=>$v){
            $v['images']=getPic($v['images']);
            $decoration_customization[$k]=$v;
        }
        //培训报班
        $train = M('train')->where($where)->order('add_time desc')->select();
        foreach ($train as $k=>$v){
            $v['images']=getPic($v['images']);
            $train[$k]=$v;
        }
        
        $arr = array(
            "code"=>0,
            "msg"=>'',
            "data"=>$recruit,
        );
        $this->ajaxReturn($arr);
        
    }
    
    
    public function getshow(){
        $get = $_GET;
        $where['id'] = $get['id'];
        $type = $get['type'];
        if($type == 1){
        //招聘求职
         $data = M('recruit')->where($where)->find();
         $mpa['id'] = $data['uid']; 
         $user = M('user')->where($mpa)->find();
         $data['tx'] = $user['avatar'];
         M('recruit')->where($where)->setInc('views');
         $data['add_time'] = date('Y-m-d',$data['add_time']);
         $data['images'] = explode(',',$data['images']);
         $arr = array(
            "code"=>0,
            "msg"=>'',
            "data"=>$data,
        );
        
        }elseif ($type == 2){
        //本地服务
         $data = M('local')->where($where)->find();
         $mpa['id'] = $data['uid']; 
         $user = M('user')->where($mpa)->find();
         $data['tx'] = $user['avatar'];
         M('local')->where($where)->setInc('views');
         $data['add_time'] = date('Y-m-d',$data['add_time']);
         $data['images'] = explode(',',$data['images']);
         $arr = array(
            "code"=>0,
            "msg"=>'',
            "data"=>$data,
        );
        }elseif($type == 3){
        //房屋信息
         $data = M('house')->where($where)->find();
         $mpa['id'] = $data['uid']; 
         $user = M('user')->where($mpa)->find();
         $data['tx'] = $user['avatar'];
         M('house')->where($where)->setInc('views');
         $data['add_time'] = date('Y-m-d',$data['add_time']);
         $data['images'] = explode(',',$data['images']);
         $arr = array(
            "code"=>0,
            "msg"=>'',
            "data"=>$data,
        );
        }elseif($type == 4){
        //生意转让
         $data = M('business_transfer')->where($where)->find();
         $mpa['id'] = $data['uid']; 
         $user = M('user')->where($mpa)->find();
         $data['tx'] = $user['avatar'];
         M('business_transfer')->where($where)->setInc('views');
         $data['add_time'] = date('Y-m-d',$data['add_time']);
         $data['images'] = explode(',',$data['images']);
         $arr = array(
            "code"=>0,
            "msg"=>'',
            "data"=>$data,
        );
        }elseif($type == 5){
        //汽车交易
         $data = M('car_trading')->where($where)->find();
         $mpa['id'] = $data['uid']; 
         $user = M('user')->where($mpa)->find();
         $data['tx'] = $user['avatar'];
         M('car_trading')->where($where)->setInc('views');
         $data['add_time'] = date('Y-m-d',$data['add_time']);
         $data['images'] = explode(',',$data['images']);
         $arr = array(
            "code"=>0,
            "msg"=>'',
            "data"=>$data,
        );

        }elseif($type == 6){
         //二手物品
         $data = M('car_trading')->where($where)->find();
         $mpa['id'] = $data['uid']; 
         $user = M('user')->where($mpa)->find();
         $data['tx'] = $user['avatar'];
         M('car_trading')->where($where)->setInc('views');
         $data['add_time'] = date('Y-m-d',$data['add_time']);
         $data['images'] = explode(',',$data['images']);
         $arr = array(
            "code"=>0,
            "msg"=>'',
            "data"=>$data,
        );
        }elseif($type == 7){
        //装修定制
         $data = M('decoration_customization')->where($where)->find();
         $mpa['id'] = $data['uid']; 
         $user = M('user')->where($mpa)->find();
         $data['tx'] = $user['avatar'];
         M('decoration_customization')->where($where)->setInc('views');
         $data['add_time'] = date('Y-m-d',$data['add_time']);
         $data['images'] = explode(',',$data['images']);
         $arr = array(
            "code"=>0,
            "msg"=>'',
            "data"=>$data,
        );
        }
        $this->ajaxReturn($arr);
         //  var_dump($data);die;
    }
    //收藏
    public function getsc(){
        $get = $_GET;
        $where['pid'] = $get['id'];
        $where['uid'] = $get['uid'];
        $type = $get['type'];
        $mpa['pid'] = $get['id'];
        $mpa['uid'] = $get['uid'];
        $mpa['type'] = $type;
        $mpa['add_date'] = time();
        $q = M('collection')->where($where)->find();
        // var_dump($q);die;
        if($q==''){
            $res = M('collection')->add($mpa);
                if ($res) {
                   $arr = array(
                    "code"=>1,
                    "msg"=>'收藏成功',
                    "data"=>''
                    );
                } else {
                    $arr = array(
                    "code"=>0,
                    "msg"=>'收藏失败',
                    "data"=>''
                    );
                }  
        }else{
            M('collection')->where($where)->delete();
            $arr = array(
                "code"=>2,
                "msg"=>'取消收藏',
                "data"=>''
             );
        }
        // var_dump($get);die;
      
       $this->ajaxReturn($arr);
        
    }
    //举报
    public function jubao(){
         $post = $_POST;
         $res = M('jubao')->add($post);
         if($res){
              $arr = array(
                "code"=>1,
                "msg"=>'发布成功',
                "data"=>''
             );
         }else{
              $arr = array(
                "code"=>0,
                "msg"=>'发布失败',
                "data"=>''
             );
         }
        $this->ajaxReturn($arr);
    }
      //获取首页公告
    public function getAnnouncement(){
        $list=M('announcement')->where('is_show=1')->limit(10)->order('id desc')->select();
        $arr = array(
            "code"=>"200",
            "msg"=>"成功",
            "data"=>$list
        );
        $this->ajaxReturn($arr);
    }

    
}